package com.vehicle_information_management_system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vehicle_information_management_system.pojo.DrivingCostInfo;
import com.vehicle_information_management_system.service.DrivingCostInfoService;
import com.vehicle_information_management_system.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/drivingCostInfo")
public class DrivingCostInfoController {
    @Autowired
    private DrivingCostInfoService drivingCostInfoService;
    @RequestMapping("/list")
    public Result<?> getDrivingCostInfo(@RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        @RequestParam(defaultValue = "") String search){
        IPage<DrivingCostInfo> page=drivingCostInfoService.getDrivingCostInfo(pageNum,pageSize,search);
        return Result.success(page);
    }
    @RequestMapping("/add")
    public Result<?> add(@RequestBody DrivingCostInfo drivingCostInfo){
        try{
            drivingCostInfoService.add(drivingCostInfo);
        }catch (Exception e){
            return Result.error("添加失败");
        }
        return Result.success("添加成功");
    }
    @RequestMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Integer id){
        try{
            drivingCostInfoService.delete(id);
        }catch (Exception e){
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }
    @RequestMapping("/update")
    public Result<?> update(@RequestBody DrivingCostInfo drivingCostInfo){
        try{
            drivingCostInfoService.update(drivingCostInfo);
        }catch (Exception e){
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }

    @RequestMapping("/getDrivingCostInfoByUserId/{userId}")
    public Result<?> getDrivingCostInfoByUserId(@PathVariable Integer userId){
        return Result.success(drivingCostInfoService.getDrivingCostInfoByUserId(userId));
    }

}
